const NewsModel = require("../../models/newsModel")

const newsService = {
    add: async ({ userId, title, content, category, cover, isPublish, editTime }) => {
        console.log(category)
        return NewsModel.create({
            userId, title, content, category, cover, isPublish, editTime
        })
    },


    updateList: async ({ _id, title, content, category, cover, isPublish, editTime }) => {
        console.log(_id)
        if (cover) {
            return NewsModel.updateOne({ _id }, {
                title, content, category, cover, isPublish, editTime
            })
        } else {
            return NewsModel.updateOne({ _id }, {
                title, content, category, isPublish, editTime
            })
        }
    },

    getList: async ({ _id }) => {
        console.log(global.userId)
        return _id ? NewsModel.find({ _id }) : NewsModel.find({userId:global.userId})
    },

    publish: async ({ _id, isPublish, editTime }) => {
        return NewsModel.updateOne({ _id }, { isPublish, editTime })
    },
    delList: async ({ _id }) => {
        return NewsModel.deleteOne({ _id })
    },
}

module.exports = newsService